package ru.timofeyco.justfeelbetter;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;

import ru.timofeyco.justfeelbetter.adapters.TypesListAdapter;
import ru.timofeyco.justfeelbetter.db.JustFeelBetterDatabase;
import ru.timofeyco.justfeelbetter.model.DrillType;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class EditTypesActivity extends Activity {
	JustFeelBetterDatabase dbHelper;
	Context context;
	
	HashMap<View, DrillType> rateControlViewToDrillType;
	HashMap<DrillType, Dialog> drillTypeToRateDialog;
	HashMap<DrillType, EditText> drillTypeToRateEditText;
	
	
	 @Override
	 public void onCreate(Bundle savedInstanceState) {
		 super.onCreate(savedInstanceState);
		 setContentView(R.layout.types_list);
		 
		 fillMallsListView();
		 
		 this.rateControlViewToDrillType = new HashMap<View, DrillType>();
		 this.drillTypeToRateDialog = new HashMap<DrillType, Dialog>();
		 this.drillTypeToRateEditText = new HashMap<DrillType, EditText>();
		 	 
		 context = this;
	 }
	 
	 @Override
	protected void onResume() {
		super.onResume();
		
		fillMallsListView();
	}
	 
	 public void onClickAddDrillType(View v) {
		 Intent addDrillIntent =  new Intent(this, AddDrillTypeActivity.class);
		 this.startActivityForResult(addDrillIntent, RESULT_OK);			
	 }
	 
	 /**
     * Fill malls ListView by malls array
     */
    private void fillMallsListView() {
    	dbHelper = new JustFeelBetterDatabase(this);
		DrillType[] drillTypes = dbHelper.getDrillTypes();
		 
    	ListView listView = (ListView) findViewById(R.id.typesListView);
		if (listView != null) {		
			ArrayList<DrillType> drillTypesList = new ArrayList<DrillType>(Arrays.asList(drillTypes));
			listView.setAdapter(new TypesListAdapter(this, R.layout.types_row, drillTypesList));
			listView.setOnItemClickListener(onClickTypeHandler);			
		}    	
    }
    
    private OnItemClickListener onClickTypeHandler = new OnItemClickListener() {
	    public void onItemClick(@SuppressWarnings("rawtypes") AdapterView parent, View v, int position, long id) {
	    	TypesListAdapter adapter = (TypesListAdapter) parent.getAdapter();
	    	DrillType drillType = adapter.getItem(position);
	    	Toast.makeText(context, drillType.getName(), Toast.LENGTH_LONG);
	    	
	    }
	};
	
	public void showRateEditingDialog(DrillType drillType, EditText outEditText) {		
		Dialog dialog = null;
		if (drillTypeToRateDialog.containsKey(drillType)) {
			dialog = drillTypeToRateDialog.get(drillType);
		} else {
			dialog = new Dialog(this);
			drillTypeToRateDialog.put(drillType, dialog);
			drillTypeToRateEditText.put(drillType, outEditText);
		}

		dialog.setContentView(R.layout.edit_rate_dialog);
		dialog.setTitle(getString(R.string.rate_dialog_title));
		
		EditText rateEdit = (EditText) dialog.findViewById(R.id.edit_rate_edittext);
		String rateText = Integer.toString(drillType.getRate());
		rateEdit.setText(rateText);
				
		Button downRateBtn = (Button) dialog.findViewById(R.id.down_rate_btn);
		downRateBtn.setOnClickListener(new OnClickListener() {			
			@Override
			public void onClick(View v) {							
				EditText rateEdit = (EditText) ((View)v.getParent()).findViewById(R.id.edit_rate_edittext);
				
				int currRate = Integer.valueOf(rateEdit.getText().toString());
				--currRate;
				if (currRate > 0) {
					rateEdit.setText(Integer.toString(currRate));
				}				
			}
		});
		
		Button upRateBtn = (Button) dialog.findViewById(R.id.up_rate_btn);
		upRateBtn.setOnClickListener(new OnClickListener() {			
			@Override
			public void onClick(View v) {							
				EditText rateEdit = (EditText) ((View)v.getParent()).findViewById(R.id.edit_rate_edittext);
				
				int currRate = Integer.valueOf(rateEdit.getText().toString());
				++currRate;
				rateEdit.setText(Integer.toString(currRate));								
			}
		});
		
		Button saveRateBtn = (Button) dialog.findViewById(R.id.save_rate_btn);
		
		View controlView = (View) saveRateBtn.getParent();
		if (!rateControlViewToDrillType.containsKey(controlView)) {
			this.rateControlViewToDrillType.put(controlView, drillType);
		}
		
		saveRateBtn.setOnClickListener(new OnClickListener() {			
			@Override
			public void onClick(View v) {							
				View parentView = (View) v.getParent();
				DrillType drillType = EditTypesActivity.this.rateControlViewToDrillType.get(parentView);				
				
				EditText rateEdit = (EditText) ((View) (parentView.getParent())).findViewById(R.id.edit_rate_edittext);		
				int currRate = Integer.valueOf(rateEdit.getText().toString());				
				drillType.setRate(currRate);
				EditTypesActivity.this.dbHelper.updateDrillType(drillType.getName(), drillType);
				
				EditText outRateEdit = EditTypesActivity.this.drillTypeToRateEditText.get(drillType);
				outRateEdit.setText(Integer.toString(currRate));
				
				Dialog dialogForCancel = (Dialog) EditTypesActivity.this.drillTypeToRateDialog.get(drillType);
				dialogForCancel.cancel();							
			}
		});
		
		Button cancelRateBtn = (Button) dialog.findViewById(R.id.cancel_rate_btn);
		cancelRateBtn.setOnClickListener(new OnClickListener() {			
			@Override
			public void onClick(View v) {
				View parentView = (View) v.getParent();
				DrillType drillType = EditTypesActivity.this.rateControlViewToDrillType.get(parentView);
				
				Dialog dialogForCancel = (Dialog) EditTypesActivity.this.drillTypeToRateDialog.get(drillType);
				dialogForCancel.cancel();
			}
		});
		
		dialog.show();
	}
}
